package com.yidu.qx.action;

import com.yidu.qx.domain.JsonM;
import com.yidu.qx.domain.QxModuleEntity;
import com.yidu.qx.service.ModuleService;
import com.yidu.qx.utils.BaseMessage;
import com.yidu.qx.utils.Pages;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @program: jurisdiction
 * @description: 模板控制器
 *
 * @author: 张超
 * @create: 2019-09-25 14:56
 */

@Controller
@RequestMapping("ModuleController")
public class ModuleController {

    Logger logger = Logger.getLogger(getClass());

    @Resource
    ModuleService moduleService;

    /**
     * 查询所有
     * @param qxModuleEntity
     * @param pages
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> findAll(QxModuleEntity qxModuleEntity, Pages pages,int page){
        pages.setCurPage(page);
        return moduleService.findAll(qxModuleEntity,pages);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<JsonM> selectAll(String type){
        QxModuleEntity qxModuleEntity = new QxModuleEntity();
        if("1".equals(type)){
            qxModuleEntity.setModId("0");
        }
        List<JsonM> list = moduleService.selectAll(qxModuleEntity);
        return list;
    }

    @ResponseBody
    @RequestMapping("addOrUpdate")
    public BaseMessage addOrUpdate(QxModuleEntity qxModuleEntity){
        BaseMessage baseMessage = new BaseMessage();

        if (StringUtils.isEmpty(qxModuleEntity.getGetTmModId())){
            qxModuleEntity.setGetTmModId("0");
        }

        if(StringUtils.isEmpty(qxModuleEntity.getModId())){
            moduleService.add(qxModuleEntity);
        }else{
            moduleService.update(qxModuleEntity);
        }

        baseMessage.setMsg("编辑成功");
        baseMessage.setState(1);

        return baseMessage;
    }

    @ResponseBody
    @RequestMapping("findById")
    public QxModuleEntity findById(String id){
        return moduleService.findById(id);
    }

    @ResponseBody
    @RequestMapping("delete")
    public BaseMessage delete(QxModuleEntity qxModuleEntity){
        BaseMessage baseMessage = new BaseMessage();
        moduleService.delete(qxModuleEntity);
        baseMessage.setState(1);
        baseMessage.setMsg("删除成功");
        return baseMessage;
    }

    @ResponseBody
    @RequestMapping("deleteAll")
    public BaseMessage deleteAll(String ids){
        BaseMessage baseMessage = new BaseMessage();
        String id[] = ids.split(",");
        for (int i = 0;i<id.length;i++){
            QxModuleEntity qxModuleEntity = new QxModuleEntity();
            qxModuleEntity.setModId(id[i]);
            moduleService.delete(qxModuleEntity);
        }

        baseMessage.setState(1);
        baseMessage.setMsg("删除成功");
        return baseMessage;
    }
}